//
//  NewFeatureViewController.swift
//  MyWeibo
//
//  Created by tanchao on 15/8/2.
//  Copyright © 2015年 chaoren. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
/// 新特性界面
class NewFeatureViewController: UICollectionViewController {
//图像总数
    let imageCount = 4
//    MARK:- 构造函数
    private let layout  = NewFeatureViewLayout()
    init(){
        super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
  
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? NewFeatureViewCell
        cell?.imageIndex = indexPath.item
        return cell!
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        if path.item == imageCount - 1{
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureViewCell
            cell.startAnimate()
        }
    }
}
/// 新特性Cell
class  NewFeatureViewCell:UICollectionViewCell {
    private var imageIndex = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
                button.hidden = true
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        prepareUI()
    }
    func startButtonClick(){

        NSNotificationCenter.defaultCenter().postNotificationName(WeiboRootViewControllerSwitchNotification, object: true)
    }
    private func prepareUI(){

        contentView.addSubview(iconView)
        contentView.addSubview(button)
        iconView.ff_Fill(contentView)
        button.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
    private func startAnimate(){
        button.hidden = false
        button.transform = CGAffineTransformMakeScale(0, 0)
        button.userInteractionEnabled = false
       UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: {
        self.button.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            self.button.userInteractionEnabled = true
        }
    }
    
//    MARK:- 懒加载控件
    lazy private  var iconView = UIImageView()
    lazy private  var button: UIButton = {
        let object = UIButton()
        object.setTitle("立即体验", forState: UIControlState.Normal)
        object.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        object.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        object.sizeToFit()
        object.hidden = true
        object.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return object
        }()
}
/// 自定义流水布局
private class  NewFeatureViewLayout : UICollectionViewFlowLayout{
    private override func prepareLayout() {
        itemSize = collectionView!.bounds.size
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        minimumLineSpacing = 0
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
}
